#include "geometryBuilder.h"
#include "geometrybuilderlua.h"
#include "../core/LuaUtils.h"
#include "h3dLuaBindings.h"

void GeometryBuilderLua::pushStaticFuncs(lua_State* L)
{
	static const struct luaL_Reg mylib [] = {
		{"Clear", li_clear},
		{"MakeMesh", li_makeMesh},
		{"BuildModel", li_buildModel},
		{"MakeTriangles", li_makeTriangles},
		{"MakeQuads", li_makeQuads},
		{"RawIdxTriangle", li_rawIdxTriangle},
		{"Vposition", li_vposition},
		{"Vtexcoords", li_vtexcoords},
		{"Vtexcoords0", li_vtexcoords0},
		{"Vtexcoords1", li_vtexcoords1},
		{"Vnormal", li_vnormal},
		{"Vtangent", li_vtangent},
		{"Vbitangent", li_vbitangent},
		{"Vweights", li_vweights},
		{"Vjindices", li_vjindices},
		{NULL, NULL}  /* sentinel */
	};

	// dump them into said table
	luaL_register(L, NULL, mylib);
}

int GeometryBuilderLua::li_clear(lua_State* L)
{
	H3DGeometryBuilder* op = (H3DGeometryBuilder*)typeCheckUnboxAO(L,1,"H3DGeometryBuilder");
	if(op == NULL){
		LOG(logERROR) << "Null self!";
	return 0;}
	op->clear();
	
	return 0;
}


int GeometryBuilderLua::li_makeMesh(lua_State* L)
{
	H3DGeometryBuilder* op = (H3DGeometryBuilder*)typeCheckUnboxAO(L,1,"H3DGeometryBuilder");
	if(op == NULL){
		LOG(logERROR) << "Null self!";
	return 0;}
	std::string a1 = lua_tostring(L, 2);
	H3DRes a2 = unboxH3DRes(L, 3);
	op->makeMesh(a1, a2);
	
	return 0;
}


int GeometryBuilderLua::li_buildModel(lua_State* L)
{
	H3DGeometryBuilder* op = (H3DGeometryBuilder*)typeCheckUnboxAO(L,1,"H3DGeometryBuilder");
	if(op == NULL){
		LOG(logERROR) << "Null self!";
	return 0;}
	H3DNode a1 = unboxH3DNode(L, 2);
	std::string a2 = lua_tostring(L, 3);
	std::string a3 = lua_tostring(L, 4);
	H3DNode ret = op->buildModel(a1, a2, a3);
	pushH3DNode(L, ret);
	return 1;
}


int GeometryBuilderLua::li_makeTriangles(lua_State* L)
{
	H3DGeometryBuilder* op = (H3DGeometryBuilder*)typeCheckUnboxAO(L,1,"H3DGeometryBuilder");
	if(op == NULL){
		LOG(logERROR) << "Null self!";
	return 0;}
	op->makeTriangles();
	
	return 0;
}


int GeometryBuilderLua::li_makeQuads(lua_State* L)
{
	H3DGeometryBuilder* op = (H3DGeometryBuilder*)typeCheckUnboxAO(L,1,"H3DGeometryBuilder");
	if(op == NULL){
		LOG(logERROR) << "Null self!";
	return 0;}
	op->makeQuads();
	
	return 0;
}


int GeometryBuilderLua::li_rawIdxTriangle(lua_State* L)
{
	H3DGeometryBuilder* op = (H3DGeometryBuilder*)typeCheckUnboxAO(L,1,"H3DGeometryBuilder");
	if(op == NULL){
		LOG(logERROR) << "Null self!";
	return 0;}
	int a1 = (int)lua_tointeger(L, 2);
	int a2 = (int)lua_tointeger(L, 3);
	int a3 = (int)lua_tointeger(L, 4);
	op->rawIdxTriangle(a1, a2, a3);
	
	return 0;
}


int GeometryBuilderLua::li_vposition(lua_State* L)
{
	H3DGeometryBuilder* op = (H3DGeometryBuilder*)typeCheckUnboxAO(L,1,"H3DGeometryBuilder");
	if(op == NULL){
		LOG(logERROR) << "Null self!";
	return 0;}
	double a1 = (double)lua_tonumber(L, 2);
	double a2 = (double)lua_tonumber(L, 3);
	double a3 = (double)lua_tonumber(L, 4);
	op->vposition(a1, a2, a3);
	
	return 0;
}


int GeometryBuilderLua::li_vtexcoords(lua_State* L)
{
	H3DGeometryBuilder* op = (H3DGeometryBuilder*)typeCheckUnboxAO(L,1,"H3DGeometryBuilder");
	if(op == NULL){
		LOG(logERROR) << "Null self!";
	return 0;}
	double a1 = (double)lua_tonumber(L, 2);
	double a2 = (double)lua_tonumber(L, 3);
	op->vtexcoords(a1, a2);
	
	return 0;
}


int GeometryBuilderLua::li_vtexcoords0(lua_State* L)
{
	H3DGeometryBuilder* op = (H3DGeometryBuilder*)typeCheckUnboxAO(L,1,"H3DGeometryBuilder");
	if(op == NULL){
		LOG(logERROR) << "Null self!";
	return 0;}
	double a1 = (double)lua_tonumber(L, 2);
	double a2 = (double)lua_tonumber(L, 3);
	op->vtexcoords0(a1, a2);
	
	return 0;
}


int GeometryBuilderLua::li_vtexcoords1(lua_State* L)
{
	H3DGeometryBuilder* op = (H3DGeometryBuilder*)typeCheckUnboxAO(L,1,"H3DGeometryBuilder");
	if(op == NULL){
		LOG(logERROR) << "Null self!";
	return 0;}
	double a1 = (double)lua_tonumber(L, 2);
	double a2 = (double)lua_tonumber(L, 3);
	op->vtexcoords1(a1, a2);
	
	return 0;
}


int GeometryBuilderLua::li_vnormal(lua_State* L)
{
	H3DGeometryBuilder* op = (H3DGeometryBuilder*)typeCheckUnboxAO(L,1,"H3DGeometryBuilder");
	if(op == NULL){
		LOG(logERROR) << "Null self!";
	return 0;}
	double a1 = (double)lua_tonumber(L, 2);
	double a2 = (double)lua_tonumber(L, 3);
	double a3 = (double)lua_tonumber(L, 4);
	op->vnormal(a1, a2, a3);
	
	return 0;
}


int GeometryBuilderLua::li_vtangent(lua_State* L)
{
	H3DGeometryBuilder* op = (H3DGeometryBuilder*)typeCheckUnboxAO(L,1,"H3DGeometryBuilder");
	if(op == NULL){
		LOG(logERROR) << "Null self!";
	return 0;}
	double a1 = (double)lua_tonumber(L, 2);
	double a2 = (double)lua_tonumber(L, 3);
	double a3 = (double)lua_tonumber(L, 4);
	op->vtangent(a1, a2, a3);
	
	return 0;
}


int GeometryBuilderLua::li_vbitangent(lua_State* L)
{
	H3DGeometryBuilder* op = (H3DGeometryBuilder*)typeCheckUnboxAO(L,1,"H3DGeometryBuilder");
	if(op == NULL){
		LOG(logERROR) << "Null self!";
	return 0;}
	double a1 = (double)lua_tonumber(L, 2);
	double a2 = (double)lua_tonumber(L, 3);
	double a3 = (double)lua_tonumber(L, 4);
	op->vbitangent(a1, a2, a3);
	
	return 0;
}


int GeometryBuilderLua::li_vweights(lua_State* L)
{
	H3DGeometryBuilder* op = (H3DGeometryBuilder*)typeCheckUnboxAO(L,1,"H3DGeometryBuilder");
	if(op == NULL){
		LOG(logERROR) << "Null self!";
	return 0;}
	double a1 = (double)lua_tonumber(L, 2);
	double a2 = (double)lua_tonumber(L, 3);
	double a3 = (double)lua_tonumber(L, 4);
	double a4 = (double)lua_tonumber(L, 5);
	op->vweights(a1, a2, a3, a4);
	
	return 0;
}


int GeometryBuilderLua::li_vjindices(lua_State* L)
{
	H3DGeometryBuilder* op = (H3DGeometryBuilder*)typeCheckUnboxAO(L,1,"H3DGeometryBuilder");
	if(op == NULL){
		LOG(logERROR) << "Null self!";
	return 0;}
	int a1 = (int)lua_tointeger(L, 2);
	int a2 = (int)lua_tointeger(L, 3);
	int a3 = (int)lua_tointeger(L, 4);
	int a4 = (int)lua_tointeger(L, 5);
	op->vjindices(a1, a2, a3, a4);
	
	return 0;
}


